<template>
  <div class="edu-page-warp choose-subject">
    <!--申请理由弹框-->
    <edu-dialog :title="$t('graduationDesign.fillApplicationReason')" width="40%" @close="onReasonCancel" :visible="dialogVisible">
      <el-form :model="reasonForm" ref="reasonForm" :rules="reasonRules" label-suffix="：" label-position="right" label-width="110px">
        <el-form-item :label="$t('graduationDesign.applyReason')" prop="applyDesc">
          <el-input v-model="reasonForm.applyDesc" type="textarea" :rows="4" maxlength="100" show-word-limit :placeholder="$t('graduationDesign.pleaseEnter')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="buttons">
        <el-button @click="onReasonCancel">{{$t('graduationDesign.cancel')}}</el-button>
        <el-button type="primary" @click="onReasonSubmit">{{$t('graduationDesign.confirm')}}</el-button>
      </div>
    </edu-dialog>

    <condition-card @reset="reset" @search="searchList" label-width="130px">
      <el-row>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.subjectCollege')">
            <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
              <el-option v-for="item in source.collegeOptions" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.subjectMajor')">
            <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
              <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.subjectName')">
            <el-input v-model="queryForm.projectName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </condition-card>

    <el-card shadow="never">
      <div class="edu-page-title">{{$t('graduationDesign.subjectList')}}</div>
      <div class="edu-opertion-bar">
        <div class="float-left">
          <el-input v-model="queryForm.teacherName" :placeholder="$t('graduationDesign.pleaseEnterTeaSearch')" style="width:300px" @keyup.enter.native="searchList" clearable>
            <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
          </el-input>
        </div>
      </div>
      <edu-table ref="subjectListTable" :url="getList" :selection="false" :order="false">
        <edu-table-column :label="$t('graduationDesign.subjectName')" prop="projectName" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.subjectType')" prop="projectType" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.tutor')" prop="teacherName" min-width="90"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.teacherCollege')" prop="teacherCollegeIdI18n" min-width="120"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.subjectCollege')" prop="collegeIdI18n" min-width="120"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.subjectMajor')" prop="subjectIdI18n" min-width="120"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.operation')" min-width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="onView(scope.row)">{{$t('graduationDesign.view')}}</el-button>
            <el-button type="text" @click="onApply(scope.row)">{{$t('graduationDesign.applyReason')}}</el-button>
          </template>
        </edu-table-column>
      </edu-table>
    </el-card>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/studentTopic";

export default {
  name: 'subjectList',
  props: {
    chooseSubjectForm: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      api: API,
      getList: '/api/designservice/optStudentInfo/queryByPageOptInfo',
      queryForm: {
        grade: '',
        collegeId: '',
        subjectId: '',
        projectName: ''
      },
      source: {
        collegeOptions: [],
        subjectOptions: []
      },
      dialogVisible: false,
      reasonForm: {
        applyDesc: ''
      },
      reasonRules: {
        applyDesc: [
          { required: true, message: this.$t('graduationDesign.pleaseEnter'), trigger: 'blur' }
        ]
      },
      actionType: ''
    };
  },
  mounted() {
    this.queryForm.grade = new Date().getFullYear() + "";
    this.getCollege();
    this.searchList();
  },
  methods: {
    searchList() {
      this.$refs.subjectListTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        grade: new Date().getFullYear() + "",
        collegeId: '',
        subjectId: '',
        projectName: ''
      };
      this.source.subjectOptions = [];
      this.searchList();
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    // 选择毕业年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val) {
        const params = {
          grade: this.queryForm.grade,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    onView(row) {
      const params = { ...row };
      this.api.getSubjectInfo(params).then(res => {
        this.$emit("choose-view", res.data);
      }).catch();
    },
    // 申请理由
    onApply(row) {
      const params = {
        studentId: row.studentId
      };
      this.dialogVisible = true;
      this.reasonForm = { ...row };
      this.$nextTick(() => {
        this.$refs.reasonForm.clearValidate();
      });
    },
    onReasonCancel() {
      this.dialogVisible = false;
    },
    // 填写申请理由 提交
    onReasonSubmit() {
      this.$refs.reasonForm.validate(valid => {
        if (valid) {
          const params = { ...this.reasonForm };
          this.api.apply(params).then(res => {
            this.$message.success(this.$t('graduationDesign.applySuccess'));
            this.dialogVisible = false;
            this.searchList();
          }).catch();
        } else {
          return false;
        }
      });
    },
    // 选择课题 确定
    onSubmit() {
      this.$emit("choose-submit", this.reasonForm);
    },
    // 选择课题 取消
    onCancel() {
      this.$emit("choose-cancel");
    },
  }
}
</script>
<style lang="scss" scoped>
.choose-subject {
    .search-item {
        width: 100%;
    }
    .bottom-line {
        border-top: 1px solid #dcdfe6;
        .bottom-btn {
            margin-top: 20px;
        }
    }
}
</style>
